<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <title>rand</title>
    <link rel="stylesheet" type="text/css" href="csound.css" />
    <meta name="generator" content="DocBook XSL Stylesheets V1.78.1" />
    <link rel="home" href="index.html" title="The Canonical Csound Reference Manual" />
    <link rel="up" href="OpcodesTop.html" title="Orchestra Opcodes and Operators" />
    <link rel="prev" href="r2c.html" title="r2c" />
    <link rel="next" href="randh.html" title="randh" />
  </head>
  <body>
    <div class="navheader">
      <table width="100%" summary="Navigation header">
        <tr>
          <th colspan="3" align="center">rand</th>
        </tr>
        <tr>
          <td width="20%" align="left"><a accesskey="p" href="r2c.html">Prev</a> </td>
          <th width="60%" align="center">Orchestra Opcodes and Operators</th>
          <td width="20%" align="right"> <a accesskey="n" href="randh.html">Next</a></td>
        </tr>
      </table>
      <hr />
    </div>
    <div class="refentry">
      <a id="rand"></a>
      <div class="titlepage"></div>
      <a id="IndexRand" class="indexterm"></a>
      <div class="refnamediv">
        <h2>
          <span class="refentrytitle">rand</span>
        </h2>
        <p>rand — 
      Generates a controlled random number series.
    </p>
      </div>
      <div class="refsect1">
        <a id="idp140770224"></a>
        <h2>Description</h2>
        <p>
      Output is a controlled random number series between -<span class="emphasis"><em>amp</em></span> and +<span class="emphasis"><em>amp</em></span>
    </p>
      </div>
      <div class="refsect1">
        <a id="idp140772288"></a>
        <h2>Syntax</h2>
        <pre class="synopsis">ares <span class="command"><strong>rand</strong></span> xamp [, iseed] [, isel] [, ioffset]</pre>
        <pre class="synopsis">kres <span class="command"><strong>rand</strong></span> xamp [, iseed] [, isel] [, ioffset]</pre>
      </div>
      <div class="refsect1">
        <a id="idp140836832"></a>
        <h2>Initialization</h2>
        <p>
      <span class="emphasis"><em>iseed</em></span> (optional, default=0.5) -- a seed value for the recursive pseudo-random formula. A value between 0 and 1 will produce an initial output of <span class="emphasis"><em>kamp * iseed</em></span>. A value greater than 1 will be seeded from the system clock. A negative value will cause seed re-initialization to be skipped. The default seed value is .5.
    </p>
        <p>
      <span class="emphasis"><em>isel</em></span> (optional, default=0) -- if zero, a 16-bit number is generated. If non-zero, a 31-bit random number is generated. Default is 0.
    </p>
        <p>
      <span class="emphasis"><em>ioffset</em></span> (optional, default=0) -- a base value added to the random result. New in Csound version 4.03.
    </p>
      </div>
      <div class="refsect1">
        <a id="idp140841040"></a>
        <h2>Performance</h2>
        <p>
      <span class="emphasis"><em>kamp, xamp</em></span> -- range over which random numbers are distributed.
    </p>
        <p>
      <span class="emphasis"><em>ares, kres</em></span> -- Random number produced.
    </p>
        <p>
      The internal pseudo-random formula produces values which are uniformly distributed over the range <span class="emphasis"><em>kamp</em></span> to <span class="emphasis"><em>-kamp</em></span>. <span class="emphasis"><em>rand</em></span> will thus generate uniform white noise with an R.M.S value of <span class="emphasis"><em>kamp / root 2</em></span>.
    </p>
        <p>
      The value <span class="emphasis"><em>ares</em></span> or <span class="emphasis"><em>kres</em></span> is within is a half-closed interval which contains <span class="emphasis"><em>-xamp</em></span>, but never contains <span class="emphasis"><em>+xamp</em></span>.
    </p>
      </div>
      <div class="refsect1">
        <a id="idp140848128"></a>
        <h2>Examples</h2>
        <p>
      Here is an example of the rand opcode. It uses the file <a class="ulink" href="examples/rand.csd" target="_top"><em class="citetitle">rand.csd</em></a>.
      </p>
        <div class="example">
          <a id="idp140849984"></a>
          <p class="title">
            <strong>Example 732. Example of the rand opcode.</strong>
          </p>
          <div class="example-contents">
            <p>See the sections <a class="link" href="UsingRealTime.html" title="Real-Time Audio"><em class="citetitle">Real-time Audio</em></a> and <a class="link" href="CommandFlags.html" title="Csound command line"><em class="citetitle">Command Line Flags</em></a> for more information on using command line flags.</p>
            <pre class="programlisting">
<span class="csdtag">&lt;CsoundSynthesizer&gt;</span>
<span class="csdtag">&lt;CsOptions&gt;</span>
<span class="comment">; Select audio/midi flags here according to platform</span>
-odac     <span class="comment">;;;realtime audio out</span>
<span class="comment">;-iadc    ;;;uncomment -iadc if RT audio input is needed too</span>
<span class="comment">; For Non-realtime ouput leave only the line below:</span>
<span class="comment">; -o rand.wav -W ;;; for file output any platform</span>
<span class="csdtag">&lt;/CsOptions&gt;</span>
<span class="csdtag">&lt;CsInstruments&gt;</span>

<span class="ohdr">sr</span> <span class="op">=</span> 44100
<span class="ohdr">ksmps</span> <span class="op">=</span> 32
<span class="ohdr">nchnls</span> <span class="op">=</span> 2
<span class="ohdr">0dbfs</span>  <span class="op">=</span> 1

<span class="oblock">instr</span> 1	<span class="comment">;same values every time</span>

krnd <span class="opc">rand</span> 100
     <span class="opc">printk</span> .5, krnd			<span class="comment">; look </span>
aout <span class="opc">oscili</span> 0.8, 440<span class="op">+</span>krnd, 1		<span class="comment">; &amp; listen</span>
     <span class="opc">outs</span> aout, aout

<span class="oblock">endin</span>

<span class="oblock">instr</span> 2	<span class="comment">;different values every time</span>

krnd <span class="opc">rand</span> 100, 10			<span class="comment">; seed from system clock</span>
     <span class="opc">printk</span> .5, krnd			<span class="comment">; look </span>
aout <span class="opc">oscili</span> 0.8, 440<span class="op">+</span>krnd, 1		<span class="comment">; &amp; listen</span>
     <span class="opc">outs</span> aout, aout

<span class="oblock">endin</span>
<span class="csdtag">&lt;/CsInstruments&gt;</span>
<span class="csdtag">&lt;CsScore&gt;</span>
<span class="stamnt">f</span> 1 0 16384 10 1	<span class="comment">;sine wave.</span>

<span class="stamnt">i</span> 1 0 1
<span class="stamnt">i</span> 2 2 1
<span class="stamnt">e</span>

<span class="csdtag">&lt;/CsScore&gt;</span>
<span class="csdtag">&lt;/CsoundSynthesizer&gt;</span>
</pre>
          </div>
        </div>
        <p><br class="example-break" />
     </p>
        <p>
      The example will produce the following output:
      </p>
        <pre class="screen">
 i   1 time     0.00067:    50.00305
 i   1 time     0.50000:    62.71362
 i   1 time     1.00000:   -89.31885

WARNING: Seeding from current time 472230558

 i   2 time     2.00067:   -70.65735
 i   2 time     2.50000:    69.15283
 i   2 time     3.00000:   -48.79761
      </pre>
        <p>
    </p>
      </div>
      <div class="refsect1">
        <a id="idp140855648"></a>
        <h2>See Also</h2>
        <p>
      <a class="link" href="randh.html" title="randh"><em class="citetitle">randh</em></a>,
      <a class="link" href="randi.html" title="randi"><em class="citetitle">randi</em></a>
    </p>
      </div>
      <div class="refsect1">
        <a id="idp140858688"></a>
        <h2>Credits</h2>
        <p>Thanks to a note from John ffitch, I changed the names of the parameters.</p>
      </div>
    </div>
    <div class="navfooter">
      <hr />
      <table width="100%" summary="Navigation footer">
        <tr>
          <td width="40%" align="left"><a accesskey="p" href="r2c.html">Prev</a> </td>
          <td width="20%" align="center">
            <a accesskey="u" href="OpcodesTop.html">Up</a>
          </td>
          <td width="40%" align="right"> <a accesskey="n" href="randh.html">Next</a></td>
        </tr>
        <tr>
          <td width="40%" align="left" valign="top">r2c </td>
          <td width="20%" align="center">
            <a accesskey="h" href="index.html">Home</a>
          </td>
          <td width="40%" align="right" valign="top"> randh</td>
        </tr>
      </table>
    </div>
  </body>
</html>
